package dth.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;
import javax.persistence.criteria.CriteriaBuilder.In;

import dth.dao.UserDAO;
import dth.dao.UserPackageDAO;
import dth.entity.User;
import dth.entity.UserPackage;
import dth.utils.DthSessionConstant;
import dth.utils.DthUtil;

@ManagedBean(name = "userPackageBean")
public class UserPackageBean {
	private List<UserPackage> listUserPackages = new ArrayList<UserPackage>();
	private int id;
	private int userID;//select combo box
	private java.util.List<SelectItem> listCmbUSer=new ArrayList<SelectItem>();
	public java.util.List<SelectItem> getListCmbUSer() {
		UserDAO userDAO=new  UserDAO();
		
		for(User user:userDAO.getListUser())
		{
			SelectItem item=new SelectItem(user.getCustomerId(),user.getCustomerName());
			listCmbUSer.add(item);
		}
		return listCmbUSer;
	}

	public void setListCmbUSer(java.util.List<SelectItem> listCmbUSer) {
		this.listCmbUSer = listCmbUSer;
	}
	public List<UserPackage> getListUserPackages() {
		System.out.println("----user id on get list:"+userID);
		UserPackageDAO userPackageDAO = new UserPackageDAO();
		if(userID!=0){
			listUserPackages=userPackageDAO.getListUserPackgeByUserId(userID);
		}
		else
			listUserPackages = userPackageDAO.getListUserPackages();
		return listUserPackages;
	}

	public void setListUserPackages(List<UserPackage> listUserPackages) {
		this.listUserPackages = listUserPackages;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public int getUserID() {
		Object object=DthUtil.getSession(DthSessionConstant.DTH_user_package_user_id);

		if(object!=null){
			System.out.println("-----user id:"+object.toString());
			userID=Integer.parseInt(object.toString());
		}
			
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}
	public UserPackageBean() {
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("package_history.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		// TODO Auto-generated constructor stub
		System.out.println("---user package bean");
		Object objectAction = DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:" + objectAction);
		if (objectAction != null) {
			if (objectAction.equals("delete")) {
				Object objectId = DthUtil.getRequest().getParameter("id");
				if (objectId != null)
				{
					id = Integer.parseInt(objectId.toString());
					
				}
					
			}
		}
		/*
		 * 
		 */
		Object objectUserId = DthUtil.getRequest().getParameter("user_id");
		if(objectUserId!=null)
		{
			System.out.println("-----get for user id:"+objectUserId);
			userID=Integer.parseInt(objectUserId.toString());
		}
	}

	public void delete() {
		UserPackageDAO userPackageDAO = new UserPackageDAO();
		try {
			UserPackage userPackage = new UserPackage();
			userPackage.setId(id);
			userPackageDAO.delete(userPackage);
			DthUtil.showMessage("Mesage", "Update success");
		} catch (Exception ex) {
			DthUtil.showMessage("Message", "Update error");
		}
	}
	public void changeUserChooser(){
		System.out.println("---user id:"+userID);
		DthUtil.setSession(DthSessionConstant.DTH_user_package_user_id, userID);
	}
}
